package com.bringway.global.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.domain.TreeNode;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.global.domain.User;
import com.bringway.global.service.MenuService;

import net.sf.json.JSONObject;

/*
* @author 作者 xuef
* @version 创建时间：2015年9月9日 下午2:32:25
* 类说明
*/
@Controller
@RequestMapping("/menu")
public class MenuController extends JsonController{
	
	@Autowired
	private MenuService menuService;
	
	@RequestMapping("/queryMenuList")
	public ModelAndView queryMenuList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		JSONObject params = new JSONObject();
		String nodeId = request.getParameter("nodeId");
		if (StringUtils.isEmpty(nodeId)) {
			nodeId="-1";
		} 
		params.put("nodeId", nodeId);
		params.put("roleId", sysUserDetails.getRoleId());
		params.put("gyh", sysUserDetails.getGyh());
		params.put("id", sysUserDetails.getId());
		
//		List<Map<String,Object>> list01 = menuService.listMenuByParent01(params);
//		List<Map<String,Object>> list02 = menuService.listMenuByRole(params);
		
		Map<String, Map<String,Object>> mapByUserId = new HashMap<String, Map<String,Object>>();
		Map<String, Map<String,Object>> mapByUserRole = new HashMap<String, Map<String,Object>>();
		Map<String, Map<String,Object>> menuMap = new HashMap<String, Map<String,Object>>();
		//if("-1".equals(nodeId)){
			mapByUserRole = menuService.listMenuByRole(params);
		//}
		mapByUserId = menuService.listMenuByParent01(params);
		menuMap.putAll(mapByUserRole);
		for(Map.Entry<String, Map<String,Object>> m :mapByUserId.entrySet()){
			menuMap.put(m.getKey(), m.getValue());
		}
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		for(Map.Entry<String, Map<String,Object>> m :menuMap.entrySet()){
			list.add(m.getValue());
		}
	    Collections.sort(list,new Comparator<Map<String,Object>>() {
			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
			/*	String s1 = (String)o1.get("ORDER_NUM");
				String s2 = (String)o2.get("ORDER_NUM");
				return s2.compareTo(s1);*/
				int s1 = MapUtils.getInteger(o1, "ORDER_NUM");
				int s2 = MapUtils.getInteger(o2, "ORDER_NUM");
				return s2-s1;
			}
		});
	   
//		Iterator<Entry<String, Map<String, Object>>> it = mapByUserId.entrySet().iterator();
//		while(it.hasNext()){
//			Entry<String, Map<String, Object>> pageInfoMap = it.next();
//			menuMap.put(pageInfoMap.getKey(), pageInfoMap.getValue());
//			
//		}
//		
//		List<Map<String, Object>> list = menuService.listMenuByParent(params);
		ArrayList nodeList = new ArrayList();
		Iterator iter = list.iterator();
		while (iter.hasNext()) {
			Map map = (Map) iter.next();
			iter.remove();
			TreeNode n = new TreeNode();
			n.setAsync(true);
			n.setText(map.get("NAME").toString());
			n.setId(map.get("ID").toString());
			n.setUrl(map.get("URL").toString());
			n.setChildct(map.get("CHILDCT").toString());
			n.setParentId(map.get("PARENT_ID").toString());
			n.setParams(MapUtils.getString(map, "PARAMS"));
			nodeList.add(n);
		}
		serialize(request,response, nodeList,true);
		return null;
	}
	
	@RequestMapping("/queryRoleList")
	public ModelAndView queryRoleList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject params = new JSONObject();
		List<Map<String, Object>> list = menuService.queryRoleList(params);
		JSONObject result = new JSONObject();
		result.put("data", list);
		result.put("total", new Integer(list.size()));
		serialize(request,response, result);
		return null;
	}
	
	@RequestMapping("/saveMenuRole")
	public ModelAndView saveMenuRole(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String menuId = RequestUtils.getStringParameter(request, "menuId");
		String menuName = RequestUtils.getStringParameter(request, "menuName");
		String menuUrl = RequestUtils.getStringParameter(request, "menuUrl");
		String menuRoleIds = RequestUtils.getStringParameter(request, "menuRoleIds");
		String menuRoleNames = RequestUtils.getStringParameter(request, "menuRoleNames");
		JSONObject params = new JSONObject();
		params.put("menuId", menuId);
		params.put("menuName", menuName);
		params.put("menuUrl", menuUrl);
		params.put("menuRoleIds", menuRoleIds);
		params.put("menuRoleNames", menuRoleNames);
		menuService.saveMenuRole(params);
		JSONObject result = new JSONObject();
		result.put("success", true);
		serialize(request,response, result);
		return null;
	}
	
	@RequestMapping("/queryMenuRoleList")
	public ModelAndView queryMenuRoleList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String menuId = RequestUtils.getStringParameter(request, "menuId");
		JSONObject params = new JSONObject();
		params.put("menuId", menuId);
//		String reuslt = menuService.queryMenuRole(params);
//		JSONObject obj = new JSONObject();
//		obj.put("success", true);
//		obj.put("data", reuslt);
		List<Map<String, Object>> list = menuService.queryMenuRoleList(params);
		serialize(request, response, list);
		return null;
	}
}
